<?php
	$_nav='service';
	$_title='服务';
	include_once 'head.php';
	$servicec=new service();	//服务类
	$service_table='zs_service';			//子时服务表

	//申请加入
	if($_POST['submit']!=''){
		if($_FILES['logo']['name']!=''){
			//设置路径
			$folder = "./upload/service/";
			$annexFolder = $folder."large/";
			$smallFolder = $folder."small/";
		
			//上传新图
			$upimg = new uploadImg();
			$logo = $upimg->upLoad('logo',$annexFolder);
			//小图
			$upimg->smallImg($annexFolder.$logo,$smallFolder);
		}
		$data=array('logo'=>$logo,'linkman'=>$_POST['linkman'],'phone'=>$_POST['phone']
					,'title'=>$_POST['title']
					,'province'=>$_POST['province'],'city'=>$_POST['city'],'county'=>$_POST['county']
					,'address'=>$_POST['address'],'intro'=>$_POST['intro'],'create_time'=>time()
					);
		$res=$commonc->insert($service_table,$data);
		$id=$commonc->insert_id();
		$msg=$id>0?'申请成功！':'申请失败！';
		$href='service_list.php';
		echo $commonc->submitAlert($msg,$href);
		exit;
	}

	//分类筛选
	if($_GET['action']=='searchClass'){
		$text=$_POST['text'];
		$class_id=$_POST['class_id'];
	
		$where=" AND class_name like '%".$text."%'";
		$where.=$class_id==''?' and level=2 ':" AND upid='".$class_id."'";
		$res=$servicec->serviceClassList($where);
		$res=$res['result'];
		if(is_array($res)){
			// $select='<option value=" ">请选择</option>';
			foreach($res as $k=>$v){
				$select.='<option value="'.$v['id'].'">'.$v['class_name'].'</option>';
			}
		}

		$arr=array('select'=>$select);
		echo json_encode($arr);
		exit;
	}

	//列表
	$where='';
	if($_POST!='' ){//二级分类
		if($_POST['class_id']!=''){
			$where.=" AND class_id='".$_POST['class_id']."'";
		}
		$smarty->assign('search',$_POST);
	}

	if($_GET['title']!='') $where.=' AND i.title like "%'.$_GET['title'].'%"';

	//服务二级分类
	if($_GET['class_id']!='' && $_GET['class_id']!=null){
		$wheres=" AND upid='".$_GET['class_id']."'";
		$nextClass=$servicec->serviceClassList($wheres);
		$nextClass=$nextClass['result'];
		if(is_array($nextClass)){
			foreach ($nextClass as $k => $v) {
				$ids.=$v['id'].",";
			}
			$ids=substr($ids,0,-1);
		}
		$ids=$ids==''?0:$ids;
		$where.=" AND class_id in (".$ids.")";
		
	}else{
		$wheres=' AND level=2 ';
		$nextClass=$servicec->serviceClassList($wheres);
		$nextClass=$nextClass['result'];
	}

	$smarty->assign('nextClass',$nextClass);
	$smarty->assign('class_id',$_GET['class_id']);
	$smarty->assign('getSearch',$_GET);

	$where.=" AND i.check='pass' ORDER BY i.create_time desc";
	$key='i.*';
	$num='5';
	$row=$servicec->serviceList($where,$key,$num);

	//服务一级分类
	$where=' AND upid=0';
	$serviceClass=$servicec->serviceClassList($where);
	$smarty->assign('serviceClass',$serviceClass['result']);

	//最新加入
	$where=" AND i.check='pass' ORDER BY i.create_time desc";
	$key='i.*';
	$num='3';
	$new=$servicec->serviceList($where,$key,$num);
	$smarty->assign('new',$new['result']);

	//省市区
	$province = $commonc->getArea("province","",$row['province']);
	$smarty->assign("province",$province);

	$smarty->assign('row',$row['result']);
	$smarty->assign('page',$row['page']);
	$smarty->display("service_list.html");
?>